package com.avic.rong.fragment.balance;


import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.avic.rong.R;
import com.avic.rong.activity.mine.CapitalSubsidiaryActivity;
import com.avic.rong.api.mine.MembershipCardApi;
import com.avic.rong.bean.mine.MembershipCardBalanceBean;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ConstantsUtils;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.flyco.tablayout.SlidingTabLayout;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
@SuppressLint("ValidFragment")
public class MemberCardFragment extends Fragment {


    @BindView(R.id.tablayout_capital)
    SlidingTabLayout mTab;
    @BindView(R.id.viewpager_capital)
    ViewPager mVp;
    @BindView(R.id.ll_member_card)
    LinearLayout ll_member_card;
    @BindView(R.id.bt_open_member)
    Button bt_open_member;
    @BindView(R.id.ll_open_member)
    LinearLayout ll_open_member;

    @BindView(R.id.tv_balance)
    TextView tv_balance;

    private ArrayList<Fragment> fragments = new ArrayList<>();

    private String[] titles = {"近一个月", "近三个月", "近半年", "近一年","全部"};

    private Integer[] integers = {1,2,3,4,0};
    private Unbinder unbinder;
    private View view;
    private double balance;

    public boolean memberCard;
    private Integer pageNumber = 1;
    private String loginToken;

    @SuppressLint("ValidFragment")
    public MemberCardFragment(boolean memberCard, double balance) {
        // Required empty public constructor
        this.balance = balance;
        this.memberCard = memberCard;
    }


    private void initData(String token) {
        MembershipCardApi membershipCardApi = new MembershipCardApi((CapitalSubsidiaryActivity) getActivity(),memberCardListener );
        membershipCardApi.setCache(false);
        membershipCardApi.setArgument(token,pageNumber,10,0);
        HttpManager.getInstance().doHttpDeal(membershipCardApi);
    }

    HttpOnNextListener<BaseResultEntity> memberCardListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)){
                //余额
                balance =  baseResultEntity.getBalance();
                int yuE = (int) balance;
                tv_balance.setText("¥"+yuE);
            }else {
                Toast toast = Toast.makeText(getContext(), baseResultEntity.getMsg(), Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_member_card, container, false);
        unbinder = ButterKnife.bind(this, inflate);

        String token = null;
        if (SharedPreferencesUtil.isLogin()){
            loginToken = SharedPreferencesUtil.getLoginToken();
            try {
                token = URLEncoder.encode(loginToken,"UTF-8");
                initData(loginToken);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        if (memberCard){//从会员卡跳转过来
            tv_balance.setText("￥"+balance);
            ll_member_card.setVisibility(View.VISIBLE);
            ll_open_member.setVisibility(View.GONE);

        }else {
            ll_open_member.setVisibility(View.VISIBLE);
            ll_member_card.setVisibility(View.GONE);
        }

        if (tv_balance.getText().toString().trim()!=null){
            ll_member_card.setVisibility(View.VISIBLE);
            ll_open_member.setVisibility(View.GONE);
        }

        for (int i = 0; i < titles.length; i++) {
            fragments.add(MemberChildFragment.getInstance(titles[i], getContext(), integers[i],loginToken));
        }


        MyPageAdapter myPageAdapter = new MyPageAdapter(getChildFragmentManager());
        mVp.setAdapter(myPageAdapter);
        mTab.setViewPager(mVp);
        mVp.setOffscreenPageLimit(fragments.size());
        mVp.setCurrentItem(0);


        return inflate;
    }



    @OnClick(R.id.bt_open_member)
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.bt_open_member:
                ll_open_member.setVisibility(View.GONE);//隐藏
                ll_member_card.setVisibility(View.VISIBLE);//显示
                break;
        }
    }

    public class MyPageAdapter extends FragmentPagerAdapter {
        public MyPageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
